<?php
if (isset($data['chi_tiet'])) {
  $ref_title = '';
  $ten_phieu_title = 'vật tư';
  $field_set_title = 'Vật tư';
  $ten_vat_tu_title = 'Tên vật tư';
  switch($data['type']) {
    case 'nhap':
      $ref_title = 'Nhà cung cấp';
      break;
    case 'xuat':
      $ref_title = 'Tổ sản xuất';
      break;
    case 'hoi':
      $ref_title = 'Nhà cung cấp';
      break;
    case 'hong':
      $ref_title = 'Đơn vị trả';
      break;
    case 'xuatdec':
      $ref_title = 'Khách hàng';
      $ten_phieu_title = 'đề C';
      $field_set_title = 'Đề C';
      $ten_vat_tu_title = 'Loại đề C';
      break;
  }
  ?>
  <div id="nhap-gia-title">Khớp lệnh phiếu <?php echo $ten_phieu_title ?> <?php echo $data['phieu']->ma_phieu ?></div>
  <form method="post" action="<?php echo base_path() . 'vat_tu/phieu_vat_tu/khop_phieu/' . $data['type'] . '/' . $data['phieu']->ma_phieu ?>" id="khop-phieu-vat-tu" class="<?php echo $data['type'] ?>">
    <div class="form-item form-select-wrapper nha-cung-cap-wrapper">
      <label><?php echo $ref_title ?>:</label>
      <?php if ($data['type'] =='hong' || ($data['type'] == 'xuatdec' && $data['phieu']->la_khach_hang)) {?>
        <label class="data-text tncc-data-text"><?php echo $data['phieu']->ref ?></label>
      <?php } else {?>
        <label class="data-text tncc-data-text"><?php echo $data['phieu']->ref . '[' . $data['phieu']->ma_ref . ']' ?></label>
      <?php }?>
      <div class="clear"></div>
    </div>
    <div class="clear"></div>
    <fieldset>
      <legend><?php echo $field_set_title ?></legend>
      <div id="form-item-wrapper">
        <?php foreach ($data['chi_tiet'] as $vat_tu) { ?>
          <?php
            if ($data['type'] == 'xuatdec') {
              $ten_vt = $vat_tu->ten_de_c;
              $ma_vt = $vat_tu->ma_de_c;
              $value_vt = $ma_vt;
            }
            else {
              $ten_vt = $vat_tu->ten_vat_tu;
              $ma_vt = $vat_tu->ma_vat_tu;
              $value_vt = $ten_vt . ' [' . $ma_vt . ']';
            }
          ?>
          <div class="form-item-row">
            <div class="form-item form-text-wrapper">
              <label><?php echo $ten_vat_tu_title ?>:</label>
              <label class="data-text tvt-data-text" id="tvt-data-text-<?php echo $vat_tu->id ?>"><?php echo $ten_vt . ' [' . $ma_vt . ']' ?></label>
            </div>
            <div class="form-item form-text-wrapper">
              <label>Số lượng:</label>
              <label class="data-text sl-data-text" id="sl-data-text-<?php echo $vat_tu->id ?>"><?php echo $vat_tu->so_luong ?></label>
            </div>
            <div class="form-item">
              <input type="hidden" id="txt-ten-vat-tu-<?php echo $vat_tu->id ?>" value="<?php echo $value_vt ?>" />
              <input type="hidden" id="txt-so-luong-<?php echo $vat_tu->id ?>" value="<?php echo $vat_tu->so_luong ?>" />
              <!--<input id="field-update-submit-<?php /*echo $vat_tu->id */?>" type="button" value="Thay đổi"
                     class="field-update-submit form-submit" onclick="show_update_phieu_form(<?php /*echo $vat_tu->id */?>)"/>-->
            <?php /*echo dt_get_ajax_loading('show-form-loading-'.$vat_tu->id) */?>
            </div>
            <div class="clear"></div>
          </div>
        <?php } ?>
      </div>
    </fieldset>
    <?php if (isset($data['phieu']->ghi_chu)) { ?>
    <div class="form-item form-textarea-wrapper">
      <label for="ghi-chu">Ghi chú</label>
      <textarea id="ghi-chu" name="ghi_chu"
                class="form-textarea <?php echo ($_POST && (string) $_POST['error']['key'] == 'ghi_chu') ? 'error'
                  : '' ?>" cols="60" rows="5"><?php echo ($_POST) ? $_POST['ghi_chu'] : $data['phieu']->ghi_chu ?></textarea>

      <div class="description">Nhập tối đa 500 ký tự. Số lượng hiện thời: <span style="color: red;"
                                                                                id="ghi-chu-length">0</span></div>
    </div>
    <?php } ?>
    <div id="submit-wrapper">
      <input id="submit" type="submit" onclick="return check_khop_phieu();" value="Khớp phiếu" class="form-submit" name="op" />
    </div>
  </form>
  <div id='update-phieu-dialog' style="display: none;">
    <div class="form-item form-text-wrapper">
      <label for="txt-ten-vat-tu"><?php echo $ten_vat_tu_title ?></label>
      <?php if ($data['type'] == 'xuatdec') { ?>
        <select id="txt-ten-vat-tu" class="form-text text scl-de-c">
          <?php foreach ($data['gia_de_c'] as $gia_de_c ) { ?>
            <option value="<?php echo $gia_de_c->ma_de_c ?>"><?php echo $gia_de_c->ten_de_c.' - ' . number_format($gia_de_c->gia_tien) ?></option>
          <?php } ?>
        </select>
      <?php } else { ?>
        <input id="txt-ten-vat-tu" type="text" value=""
               class="form-text form-autocomplete text txt-ten-vat-tu" autocomplete="OFF"/>
        <input class="autocomplete" type="hidden" id="txt-ten-vat-tu-autocomplete"
             value="<?php echo base_path() . 'vat_tu/autocomplete' ?>" disabled="disabled"/>
      <?php } ?>
      <div class="clear"></div>
    </div>
    <div class="form-item form-text-wrapper">
      <label for="txt-so-luong">Số lượng</label>
      <input id="txt-so-luong" type="text" class="form-text text txt-so-luong" value="" autocomplete="OFF"/>
      <div class="clear"></div>
    </div>
    <div class="form-item">
      <input type="hidden" id="txt-type" value="<?php echo $data['type'] ?>" />
      <input type="hidden" id="txt-id" value="" />
      <input type="button" onclick="update_phieu_chi_tiet_vat_tu();" value="Cập nhật" class="form-submit"/>
      <input type="button" onclick="close_update_phieu_form();" value="Hủy bỏ" class="form-submit"/>
      <?php echo dt_get_ajax_loading('update-loading') ?>
    </div>
    <div class="clear"></div>
  </div>
  <?php if ($data['type'] == 'xuatdec') { ?>
    <?php foreach ($data['gia_de_c'] as $gia_de_c) { ?>
      <input type="hidden" id="ten-de-c-hidden-<?php echo $gia_de_c->ma_de_c ?>" value="<?php echo $gia_de_c->ten_de_c . ' [' . $gia_de_c->ma_de_c . ']' ?>" />
    <?php } ?>
  <?php } ?>
<?php } ?>